package com.jiangtai.training.ui.task

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.jiangtai.training.R
import com.jiangtai.training.base.BaseFragment
import com.jiangtai.training.bean.Person
import com.jiangtai.training.bean.Project
import com.jiangtai.training.bean.TaskBean
import com.jiangtai.training.constant.Constant
import com.jiangtai.training.dialog.LoadingDialog
import com.jiangtai.training.dialog.SelectWayDialog
import com.jiangtai.training.event.ClearEvent
import com.jiangtai.training.event.RefreshTaskState
import com.jiangtai.training.net.CallbackListObserver
import com.jiangtai.training.net.MyRetrofit
import com.jiangtai.training.net.ThreadSwitchTransformer
import com.jiangtai.training.net.toJsonBody
import com.jiangtai.training.response.TaskDetailModel
import com.jiangtai.training.response.TaskListModel
import com.jiangtai.training.ui.taskinfo.TaskInfoActivity
import com.jiangtai.training.util.FileUtil
import com.jiangtai.training.util.Preference
import com.jiangtai.training.util.RegexUtil
import com.jiangtai.training.util.ToJsonUtil
import com.jt.poi.PoiImport
import com.orhanobut.logger.Logger
import dismissLoading
import kotlinx.android.synthetic.main.activity_all_task.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.litepal.LitePal
import org.litepal.extension.findAll
import showLoading
import java.io.File

/**
 * Created by heCunCun on 2021/3/9
 */
class TaskFragment : BaseFragment() {
    private var serverIp: String by Preference(Constant.SERVER_IP, "")
    private var phoneId: String by Preference(Constant.PHONE_ID, "")
    private var currentTaskId: String by Preference(Constant.CURRENT_TASK_ID, "")
    override fun useEventBus(): Boolean = true

    private var list = mutableListOf<TaskBean>()

    private val taskAdapter by lazy {
        TaskAllAdapter()
    }

    override fun attachLayoutRes(): Int = R.layout.activity_all_task

    override fun initView(view: View) {
        rv_task.run {
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(requireContext())
            adapter = taskAdapter
        }

        taskAdapter.setOnItemClickListener { adapter, view, position ->
            if (phoneId.isEmpty()) {
                ToastUtils.showShort("请先设置手持机ID")
                return@setOnItemClickListener
            }
            if (currentTaskId.isEmpty()){//无当前任务可进
                val intent = Intent(requireContext(), TaskInfoActivity::class.java)
                intent.putExtra("taskId", list[position].taskId)
                startActivity(intent)
            }else{
                if (currentTaskId ==list[position].taskId){//只当前任务可进
                    val intent = Intent(requireContext(), TaskInfoActivity::class.java)
                    intent.putExtra("taskId", list[position].taskId)
                    startActivity(intent)
                }else{
                    ToastUtils.showShort("请先结束当前正在进行的任务")
                }
            }


        }

        tv_add_task.setOnClickListener {
            val dialog = SelectWayDialog(requireContext())
            dialog.show()
            dialog.setonConfirmListen(object : SelectWayDialog.OnConfirmListener {
                override fun onConfirm(way: Int) {
                    if (way == Constant.LOC) {
                        getTaskFromLocal()
                    } else {
                        if (serverIp.isEmpty() || !RegexUtil.isURL(serverIp)){
                            ToastUtils.showShort("请先设置正确的服务器IP")
                        }else{
                            getTaskFromServer()
                        }

                    }
                    dialog.dismiss()
                }

            })
        }
    }

    private fun getTaskFromLocal() {
        if (checkPermissions(
                arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                )
            )
        ) {
            openFile()
        } else {
            requestPermission(
                arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ), 0x09
            )
        }
    }

    private fun getTaskFromServer() {
        requireActivity().showLoading()
        val taskList = MyRetrofit.instance.api.getTaskList("1", "1000")
        taskList.compose(ThreadSwitchTransformer())
            .subscribe(object : CallbackListObserver<TaskListModel>() {
                override fun onSucceed(taskListModel: TaskListModel) {
                    if (taskListModel.errorCode == Constant.SUCCESSED_CODE) {
                        val tasks = taskListModel.data
                        for (task in tasks) {
                            //任务入库
                            val taskBean = TaskBean(null, null, task.taskId, task.taskName)
                            taskBean.save()
                            val projectList = task.subjects
                            for (projectBean in projectList) {
                                //科目入库
                                val project = Project(
                                    projectBean.subjectId,
                                    projectBean.subjectNm,
                                    Constant.STATE_PRE,
                                    task.taskId,
                                    0,
                                    0
                                )
                                project.save()
                            }
                            //任务人员入库
                            getPersonWithTaskId(task.taskId)
                        }
                        //  刷新数据
                        initData()
                    }
                    requireActivity().dismissLoading()
                }

                override fun onFailed() {
                    requireActivity().dismissLoading()
                }
            })
    }

    private fun getPersonWithTaskId(taskId: String) {
        val map = mapOf<String,Any>("taskId" to taskId)
        val taskDetailCall = MyRetrofit.instance.api.getTaskDetail(toJsonBody(map))
        taskDetailCall.compose(ThreadSwitchTransformer()).subscribe(object :CallbackListObserver<TaskDetailModel>(){
            override fun onSucceed(taskDetailModel: TaskDetailModel) {
                if (taskDetailModel.errorCode == Constant.SUCCESSED_CODE){
                    val taskPersons = taskDetailModel.data.persons
                    for (taskPerson in taskPersons){
                        val person = Person("","",taskPerson.personNm,taskPerson.personId,"","",0,Constant.STATE_PRE,taskId,"","")
                        person.save()
                    }
                }

            }

            override fun onFailed() {
                requireActivity().dismissLoading()
            }
        })

    }

    private fun openFile() {
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.type = "*/*"//无类型限制
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        startActivityForResult(intent, 1)
    }

    override fun lazyLoad() {
        initData()
    }

    private fun initData() {
        list = LitePal.findAll<TaskBean>()
        if (list.isNotEmpty()) {
            taskAdapter.setNewData(list)
        } else {
            taskAdapter.setEmptyView(R.layout.empty_view, rv_task)
        }

    }

    private var path: String? = ""

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val uri = data?.data
            //uri =content://com.android.externalstorage.documents/document/911E-577F%3A%E6%89%8B%E6%8C%81%E6%9C%BA%E5%AF%BC%E5%85%A5%E5%90%8D%E5%8D%95.xls
            // uri =content://media/external/file/2104
            //uri = content://media/external/file/2798
            Log.e("HCC", "uri =$uri")
            if ("file".equals(uri?.scheme, true)) {
                path = uri?.path
                Log.e("HCC", "PATH 1=$path")
            } else {
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                    path = FileUtil.getPath(requireContext(), uri!!)
                    Log.e("HCC", "PATH2 =$path")
                } else {
                    path = FileUtil.getRealPathFromURI(uri, requireContext())
                    Log.e("HCC", "PATH3 =$path")
                }
            }
            //是表格则处理
            if (path!!.endsWith(".xls") || path!!.endsWith(".xlsx")) {
                requireContext().showLoading()
                Thread {
                    decoderXlsFile(path!!)
                    try {

                    } catch (e: Throwable) {
                        requireContext().dismissLoading()
                        ToastUtils.showLong("解析失败,请确认表格格式")
                    }

                }.start()

            } else {
                ToastUtils.showShort("新建任务失败,仅支持处理表格文件")
            }
        }
    }

    private fun decoderXlsFile(path: String) {
        val task = PoiImport.ExcelReader(File(path))
        val jsonStr = ToJsonUtil.getInstance().toJson(task)
        Logger.e(jsonStr)
        val taskBean = ToJsonUtil.fromJson(jsonStr, TaskBean::class.java)
        val list = LitePal.where("taskId=?", task.taskId).find(TaskBean::class.java)
        if (list.isNotEmpty()){
            ToastUtils.showShort("不可重复导入相同任务")
            requireContext().dismissLoading()
            return
        }

        //插入数据库
        val save = taskBean.save()
        Logger.e("save=$save")
        val projectList = taskBean.projects
        val personList = taskBean.persons
        if (projectList != null) {
            for (project in projectList) {
                project.taskId = taskBean.taskId
                project.save()
            }
        }
        if (personList != null) {
            for (person in personList) {
                person.taskId = taskBean.taskId
                person.state = Constant.STATE_PRE
                person.save()
            }
        }
        //插入完成  刷新表格
        requireActivity().runOnUiThread {
            requireContext().dismissLoading()
            initData()
        }

    }


    companion object {
        fun getInstance(): TaskFragment = TaskFragment()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refreshTaskState(e: RefreshTaskState) {
        initData()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refreshTaskState(e:ClearEvent) {
        list.clear()
        taskAdapter.setNewData(list)
    }
}